package com.tupu.service.impl.dispatch;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.tupu.entity.dispatch.DadsBay;
import com.tupu.entity.dispatch.DadsDevice;
import com.tupu.entity.dispatch.DadsDevline;
import com.tupu.entity.dispatch.DadsStation;
import com.tupu.mapper.dispatch.*;
import com.tupu.service.dispatch.IOcsDataSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OcsDataSyncServiceImpl implements IOcsDataSyncService {

    @Autowired
    private OcsDataSyncMapper ocsDataSyncMapper;
    @Autowired
    private DadsBayMapper dadsBayMapper;
    @Autowired
    private DadsDeviceMapper dadsDeviceMapper;
    @Autowired
    private DadsDevlineMapper dadsDevlineMapper;
    @Autowired
    private DadsStationMapper dadsStationMapper;


    @Override
    public void syncBay() {
        List<DadsBay> dadsBayList = ocsDataSyncMapper.queryBay();
        if (!CollectionUtils.isEmpty(dadsBayList)) {
            this.insertBayData(dadsBayList);
        }
    }

    @DSTransactional
    public void insertBayData(List<DadsBay> dadsBayList) {
        List<DadsBay> tempList = new ArrayList<>();
        for (int i = 0; i < dadsBayList.size(); i++) {
            tempList.add(dadsBayList.get(i));
            if (tempList.size() == 500) {
                List<Integer> ids = tempList.stream().map(j -> j.getC0017Bayid()).collect(Collectors.toList());
                dadsBayMapper.deleteBatchIds(ids);
                dadsBayMapper.insertBatchSomeColumn(tempList);
                tempList = new ArrayList<>();
            }
        }
        if (!CollectionUtils.isEmpty(tempList)) {
            List<Integer> ids = tempList.stream().map(j -> j.getC0017Bayid()).collect(Collectors.toList());
            dadsBayMapper.deleteBatchIds(ids);
            dadsBayMapper.insertBatchSomeColumn(tempList);
        }
    }

    @Override
    public void syncDevice() {
        List<DadsDevice> dadsDeviceList = ocsDataSyncMapper.queryDevice();
        if (!CollectionUtils.isEmpty(dadsDeviceList)) {
            this.insertDeviceData(dadsDeviceList);
        }
    }

    @DSTransactional
    public void insertDeviceData(List<DadsDevice> dadsDeviceList) {
        List<DadsDevice> tempList = new ArrayList<>();
        for (int i = 0; i < dadsDeviceList.size(); i++) {
            tempList.add(dadsDeviceList.get(i));
            if (tempList.size() == 500) {
                List<Integer> ids = tempList.stream().map(j -> j.getC1211Devid()).collect(Collectors.toList());
                dadsDeviceMapper.deleteBatchIds(ids);
                dadsDeviceMapper.insertBatchSomeColumn(tempList);
                tempList = new ArrayList<>();
            }
        }
        if (!CollectionUtils.isEmpty(tempList)) {
            List<Integer> ids = tempList.stream().map(j -> j.getC1211Devid()).collect(Collectors.toList());
            dadsDeviceMapper.deleteBatchIds(ids);
            dadsDeviceMapper.insertBatchSomeColumn(tempList);
        }
    }

    @Override
    public void syncDevLine() {
        List<DadsDevline> dadsDevlineList = ocsDataSyncMapper.queryDevline();
        if (!CollectionUtils.isEmpty(dadsDevlineList)) {
            this.insertDeviceLineData(dadsDevlineList);
        }
    }

    @DSTransactional
    public void insertDeviceLineData(List<DadsDevline> dadsDevlineList) {
        List<DadsDevline> tempList = new ArrayList<>();
        for (int i = 0; i < dadsDevlineList.size(); i++) {
            tempList.add(dadsDevlineList.get(i));
            if (tempList.size() == 500) {
                List<Integer> ids = tempList.stream().map(j -> j.getC1211Devid()).collect(Collectors.toList());
                dadsDevlineMapper.deleteBatchIds(ids);
                dadsDevlineMapper.insertBatchSomeColumn(tempList);
                tempList = new ArrayList<>();
            }
        }
        if (!CollectionUtils.isEmpty(tempList)) {
            List<Integer> ids = tempList.stream().map(j -> j.getC1211Devid()).collect(Collectors.toList());
            dadsDevlineMapper.deleteBatchIds(ids);
            dadsDevlineMapper.insertBatchSomeColumn(tempList);
        }
    }

    @Override
    public void syncStation() {
        List<DadsStation> dadsStationList = ocsDataSyncMapper.queryStation();
        if (!CollectionUtils.isEmpty(dadsStationList)) {
            this.insertStationData(dadsStationList);
        }
    }

    @DSTransactional
    public void insertStationData(List<DadsStation> dadsStationList) {
        List<DadsStation> tempList = new ArrayList<>();
        for (int i = 0; i < dadsStationList.size(); i++) {
            tempList.add(dadsStationList.get(i));
            if (tempList.size() == 500) {
                List<Integer> ids = tempList.stream().map(j -> j.getC0003StationNo()).collect(Collectors.toList());
                dadsStationMapper.deleteBatchIds(ids);
                dadsStationMapper.insertBatchSomeColumn(tempList);
                tempList = new ArrayList<>();
            }
        }
        if (!CollectionUtils.isEmpty(tempList)) {
            List<Integer> ids = tempList.stream().map(j -> j.getC0003StationNo()).collect(Collectors.toList());
            dadsStationMapper.deleteBatchIds(ids);
            dadsStationMapper.insertBatchSomeColumn(tempList);
        }
    }

    /**
     * 每天凌晨三点执行
     */
    @Scheduled(cron = "0 0 3 * * ?")
//    @Scheduled(cron = "0 56 * * * ?")
    public void syncData(){
        this.syncBay();
        this.syncDevice();
        this.syncStation();
        this.syncDevLine();
    }
}
